#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e6 + 5;

int n, m;
int rt[N], pos[N];
int f[N];
void init() {
  rep(i, 1, n) f[i] = i + n, f[i + n] = i + n, rt[i] = i + n, pos[i + n] = i;
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void merge(int x, int y) {  // move x to y
  int fx = find(x), fy = find(y);
  if (fx == fy) return;
  f[fx] = fy;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  init();
  while (m--) {
    int op, a, b;
    cin >> op;
    if (op == 1) {
      cin >> a >> b;
      int fa = find(a);
      f[a] = rt[b];
    } else if (op == 2) {
      cin >> a >> b;
      swap(rt[a], rt[b]);
      pos[rt[a]] = a;
      pos[rt[b]] = b;
    } else {
      cin >> a;
      int rt = find(a);
      b = pos[rt];
      cout << b << endl;
    }
  }
  return 0;
}